package com.zhenghaijin.playbackservice.service.impl;


import com.zhenghaijin.playbackservice.mapper.PlaybackRecordMapper;
import com.zhenghaijin.playbackservice.model.PlaybackRecord;
import com.zhenghaijin.playbackservice.service.PlaybackService;
import com.zhenghaijin.playbackservice.exception.DataInsertionException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class PlaybackServiceImpl implements PlaybackService {

    private final PlaybackRecordMapper playbackRecordMapper;

    public PlaybackServiceImpl(PlaybackRecordMapper playbackRecordMapper) {
        this.playbackRecordMapper = playbackRecordMapper;
    }

    @Override
    public List<PlaybackRecord> getPlaybackRecordsByUserId(Long userId) {
        Assert.notNull(userId, "用户ID不能为空");
        log.debug("开始查询用户[{}]播放记录", userId);
        List<PlaybackRecord> records = playbackRecordMapper.findByUserId(userId);
        log.info("成功获取用户[{}] {} 条播放记录", userId, records.size());
        return records;
    }

    @Override
    public PlaybackRecord addPlaybackRecord(PlaybackRecord record) {
        validatePlaybackRecord(record);
        log.debug("开始插入播放记录: {}", record);
        
        if (playbackRecordMapper.insert(record) == 0) {
            log.error("播放记录插入失败，用户ID: {}", record.getUserId());
            throw new DataInsertionException("播放记录创建失败");
        }
        
        log.info("成功创建播放记录ID[{}], 用户[{}]", record.getId(), record.getUserId());
        return record;
    }

    @Override
    public void updatePlaybackRecord(PlaybackRecord record) {
        validatePlaybackRecord(record);
        log.debug("开始更新播放记录ID[{}]", record.getId());
        if (playbackRecordMapper.update(record) == 0) {
            log.error("播放记录更新失败，记录ID: {}", record.getId());
            throw new DataInsertionException("播放记录更新失败");
        }
        log.info("成功更新播放记录ID[{}]", record.getId());
    }

    @Override
    public void deletePlaybackRecord(Long id) {
        Assert.notNull(id, "记录ID不能为空");
        log.debug("开始删除播放记录ID[{}]", id);
        if (playbackRecordMapper.deleteById(id) == 0) {
            log.error("播放记录删除失败，记录ID: {}", id);
            throw new DataInsertionException("播放记录删除失败");
        }
        log.info("成功删除播放记录ID[{}]", id);
    }

    private void validatePlaybackRecord(PlaybackRecord record) {
        Assert.notNull(record, "播放记录对象不能为空");
        Assert.notNull(record.getUserId(), "用户ID必须填写");
        Assert.isTrue(record.getUserId() > 0, "用户ID必须为正整数");
        Assert.notNull(record.getAnimeId(), "动漫ID必须填写"); 
        Assert.isTrue(record.getAnimeId() > 0, "动漫ID必须为正整数");
        Assert.notNull(record.getWatchedAt(), "观看时间必须填写");
        Assert.isTrue(record.getWatchedAt().isBefore(LocalDateTime.now()), "观看时间不能超过当前时间");
        if (record.getEpisode() != null) {
            Assert.isTrue(record.getEpisode() <= 1000, "剧集数不能超过1000");
        }
        Assert.isTrue(record.getEpisode() == null || record.getEpisode() > 0, "剧集数必须为正整数");
    }
}
